package com.ccb.lms.action;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.ccb.lms.dto.ReaderBorrowCopyBookAuthority;
import com.ccb.lms.facade.FacadeService;
import com.ccb.lms.model.Borrow;
import com.ccb.lms.model.Reader;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class ReaderAction1 extends AbstractAction implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private FacadeService facadeService;
	private List<ReaderBorrowCopyBookAuthority> readerCurrentBorrowDetailsList;
	ReaderBorrowCopyBookAuthority readerBorrowCopyBookAuthority;



	public ReaderBorrowCopyBookAuthority getReaderBorrowCopyBookAuthority() {
		return readerBorrowCopyBookAuthority;
	}


	public void setReaderBorrowCopyBookAuthority(
			ReaderBorrowCopyBookAuthority readerBorrowCopyBookAuthority) {
		
		this.readerBorrowCopyBookAuthority = readerBorrowCopyBookAuthority;
	}

	@Override
	public String execute() throws Exception {	

		return "SUCCESS";
	}
	
	
	public String GetReaderCurrentBorrowDetailsList() throws SQLException {
		Reader reader = (Reader)session.get("reader");
		readerCurrentBorrowDetailsList = facadeService.getReaderService().getReaderCurrentBorrowDetails(reader.getReaderNO());
		
		return "SUCCESS";				
	}
	
	
	public String ReBorrow() throws SQLException{
		//facadeService.getBorrowService().updateBorrow(borrow);
		
		if( readerBorrowCopyBookAuthority.getBorrowReBorrowTimes() <= readerBorrowCopyBookAuthority.getAuthorityReBorrowTimes() ){//查看续借次数是否已经达到
			Date date = new Date();
			Timestamp newDeadline = new Timestamp(date.getYear(), date.getMonth(), date.getDate() + readerBorrowCopyBookAuthority.getAuthorityReBorrowDays(), date.getHours(), date.getMinutes(), date.getSeconds(), 0);
			if(newDeadline.after(Timestamp.valueOf(readerBorrowCopyBookAuthority.getBorrowDeadline()))){
				
				Borrow borrow = readerBorrowCopyBookAuthority.getBorrow();
				borrow.setBorrowDeadline(newDeadline.toString());
				
				System.out.println(borrow.toString());
				
				facadeService.getBorrowService().updateBorrow(borrow);
				return "SUCCESS";	
			}else{
				return "NONEED";
			}

		}else{
			return "FAILURE";
		}

	}
	
	public FacadeService getFacadeService() {
		return facadeService;
	}

	public void setFacadeService(FacadeService facadeService) {
		this.facadeService = facadeService;
	}

	public List<ReaderBorrowCopyBookAuthority> getReaderCurrentBorrowDetailsList() {
		return readerCurrentBorrowDetailsList;
	}

	public void setReaderCurrentBorrowDetailsList(
			List<ReaderBorrowCopyBookAuthority> readerCurrentBorrowDetailsList) {
			this.readerCurrentBorrowDetailsList = readerCurrentBorrowDetailsList;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public void prepare() throws Exception {
		// TODO Auto-generated method stub

	}
	
	
}
